#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<time.h>

/*	Application of Monte Carlo simulation: Black Scholes model	*/

struct BoxMullerState
{
        double x1, x2, w, y1, y2;
        int useLast;
        struct drand48_data random;
};

void initBoxMullerState(struct BoxMullerState* state)
{
        state->random.__init = 0;
        state->useLast = 0;
        
        struct timeval now;
	gettimeofday(&now, NULL);
	state->random.__x[0] = now.tv_usec;
}

double boxMullerRandom(struct BoxMullerState* state)
{
        double randomNumber;

        if (state->useLast)
        {
                state->y1 = state->y2;
                state->useLast = 0;
        }
        else
        {
                do
                {
                        drand48_r(&state->random, &state->x1);
                        state->x1 = 2.0 * state->x1 - 1.0;
                        drand48_r(&state->random, &state->x2);
                        state->x2 = 2.0 * state->x2 - 1.0;
                        state->w = state->x1 * state->x1 + state->x2 * state->x2;
                }
                while (state->w >= 1.0);

                state->w = sqrt((-2.0 * log(state->w)) / state->w);
                state->y1 = state->x1 * state->w;
                state->y2 = state->x2 * state->w;
                state->useLast = 1;
        }

        return state->y1;
}

double max(double a, double b){
	if(a > b)
		return a;
	else
		return b;
}

double variance(double * trials, double mean, double M){
	int i;
	double sum = 0;
	for(i = 0; i < M; i++)
		sum += (trials[i] - mean) * (trials[i] - mean);
	
	return sum/M;
}

int main(int argc, char **argv){
	
	double S, E, r, sigma, T, M;

	scanf("%lf %lf %lf %lf %lf %lf", &S, &E, &r, &sigma, &T, &M);

	double *trials = (double *) malloc(sizeof(double)*M); 

	struct BoxMullerState state;

	initBoxMullerState(&state);

	int i;
	double number, t, mean = 0;
	for(i = 0; i < M; i++){
		
		number = boxMullerRandom(&state);

		t = S * exp( (r - (sigma * sigma) / 2.0 ) * T + sigma * sqrt(T) * number);

		trials[i] = exp( -r * T ) * max(t - E, 0);

		mean += trials[i];
	}

	mean = mean/M;
	double stddev = sqrt (variance(trials, mean, M));
	double confwidth = 1.96 * stddev/sqrt(M);
	double confmin = mean - confwidth;
	double confmax = mean + confwidth;

	printf("S	%.lf\n", S);
	printf("E	%.lf\n", E);
	printf("r	%.lf\n", r);
	printf("sigma	%.lf\n", sigma);
	printf("T	%.lf\n", T);
	printf("M	%.lf\n", M);
	printf("Confidence interval: (%.6lf, %.6lf)\n", confmin, confmax);	

	free(trials);
	
	return 0;
}
